home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0013_Command Line Parsing.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  4KB  |  126 lines

  1. {
  2. From: dissel@nunic.nu.edu (David S. Issel)
  3.  
  4. Someone was looking for command line parsing in turbo pascal.
  5.  
  6. This is a unit that I wrote years ago.
  7.  
  8. To use it, simply put the USES CMDLINE; in your program.
  9.  
  10. Example1:  If you entered:  MYPROG /x/y/z="this is a test"
  11.  
  12. TurboPascal would respond:  ParamStr(x) Contents
  13.                             =========== =================
  14.                             1           /x/y/z="this
  15.                             2           is
  16.                             3           a
  17.                             4           test"
  18.  
  19. My unit would respond:      1           /X
  20.                             2           /Y
  21.                             3           /Z=this is a test
  22.  
  23.  
  24. Example2:  If you entered:  MYPROG file1,file2,file3
  25.  
  26. TurboPascal would respond:  ParamStr(x) Contents
  27.                             =========== =================
  28.                             1           file1,file2,file3
  29.  
  30. My unit would respond:      1           FILE1
  31.                             2           FILE2
  32.                             3           FILE3
  33.  
  34. My unit replaces the ParamCount variable and ParamStr() function.
  35. The original TurboPascal routines are retained as System.ParamCount and
  36. System.ParamStr()
  37.  
  38. Try it, you'll like it... (I swear!)
  39.  
  40.  
  41. -------- cut here ------------- cmdline.pas ---------------------
  42. }
  43. Unit CMDLINE;  { Written by David S. Issel, 1989 }
  44.  
  45. Interface  { public }
  46.  
  47. Var ParamCount:integer;
  48.  
  49. Function ParamStr(Param:word):string;
  50.  
  51. Implementation  { private }
  52.  
  53. Var
  54.   ParamArray:array[1..62] of string[127];
  55.  
  56. Function ParamStr;
  57.   begin
  58.     if Param<=ParamCount
  59.       then ParamStr:=ParamArray[Param]
  60.       else ParamStr:='';
  61.   end;
  62.  
  63. Procedure SetupParamArray;
  64.   var
  65.     Index:word;
  66.     WorkStr:string;
  67.   procedure TxfrString;
  68.     var
  69.       SrchChar:string;
  70.     begin
  71.       SrchChar:=WorkStr[Index];
  72.       Inc(Index);
  73.       while (Index<=Length(WorkStr)) and (WorkStr[Index]<>SrchChar) do
  74.         begin
  75.           ParamArray[ParamCount]:=ParamArray[ParamCount]+WorkStr[Index];
  76.           Inc(Index);
  77.         end;
  78.       if Index<=Length(WorkStr)
  79.         then Inc(Index);
  80.     end;
  81.   begin
  82.     ParamCount:=0;
  83.     if System.ParamCount<1 then Exit;
  84.     WorkStr:=System.ParamStr(1);
  85.     if System.ParamCount>1
  86.       then for Index:=2 to System.ParamCount do 
  87.               WorkStr:=WorkStr+' '+System.ParamStr(Index);
  88.     Index:=1;
  89.     repeat
  90.       Inc(ParamCount);
  91.       ParamArray[ParamCount]:='';
  92.       if (WorkStr[Index]=#34) or (WorkStr[Index]=#39)
  93.         then TxfrString
  94.         else
  95.           begin
  96.             if WorkStr[Index]<>','
  97.               then ParamArray[ParamCount]:=ParamArray[ParamCount]+
  98.                                            Upcase(WorkStr[Index]);
  99.             Inc(Index);
  100.             if Index<=Length(WorkStr)
  101.               then
  102.                 begin
  103.                   while (Index<=Length(WorkStr)) and (WorkStr[Index]<>#47)
  104.                       and (WorkStr[Index]<>#32) and (WorkStr[Index]<>#34)
  105.                       and (WorkStr[Index]<>#39) and (WorkStr[Index]<>#44)
  106.                     do
  107.                       begin
  108.                         ParamArray[ParamCount]:=ParamArray[ParamCount]+
  109.                                                 Upcase(WorkStr[Index]);
  110.                         Inc(Index);
  111.                       end;
  112.                   if (Index<=Length(WorkStr)) and ((WorkStr[Index]=#34)
  113.                       or (WorkStr[Index]=#39))
  114.                     then TxfrString;
  115.                 end;
  116.           end;
  117.       while (Index<=Length(WorkStr)) and (WorkStr[Index]=#32) do
  118.         Inc(Index);
  119.     until Index>Length(WorkStr);
  120.   end;
  121.  
  122. begin  { Initialization Code }
  123.   SetupParamArray;
  124. end.
  125.   
  126.